package com.ssb.plugins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.MD5Utils;
import com.apache.tools.StrUtil;
import com.ssb.service.check.CheckedFrontSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子发票下载接口
 */
@Component
public class InvoiceDownload extends HheSuperPlugin {


    @Autowired
    private CheckedFrontSysService checkedFrontSysService;

    @Override
    public Object beforeInvoke(Map<String, Object> map) {

        //校验此前端系统是否存在且有效
        String frontSysCode = StrUtil.doNull(String.valueOf(map.get("frontSysCode")));
        Map<String,String> results =  checkedFrontSysService.checkedSysByFrontSysCode(frontSysCode);
        if (results.get("errorCode").equals("F")){
            throw new BusinessException("此系统有误：" + results.get("errorMsg"));
        }

        String infoId=String.valueOf(map.get("infoId"));
        if(StrUtil.isNull(infoId)){
            throw new BusinessException("订单ID不能为空");
        }
        String sql = "select info_id, invoice_down_url,invoice_time,invoice_no from ss_insur_order where info_id='"+infoId+"'";
        List list = (List) IusPluginUtil.getInstance().select(sql,0,0);
        Map result = new HashMap();
        result.put("errorCode","0001");
        result.put("errorMsg","下载电子发票失败");
        if(!Validator.isEmpty(list)){
            Map obj = (Map)list.get(0);
            if(Validator.isNotNull(String.valueOf(obj.get("invoiceDownUrl")))){
                result.put("data",list.get(0));
                result.put("errorMsg","下载电子发票成功");
                result.put("errorCode", "0000");
            }else {
                result.put("errorMsg", "电子发票上未开据");
            }
        }
        map.put("resultData",result);
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
